package org.chen.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.chen.enu.BatchStatus;
import org.chen.mapper.CommentBatchMapper;
import org.chen.model.CommentBatch;
import org.chen.service.impl.BatchStatsManager;
import org.chen.service.impl.CommentBatchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import reactor.util.annotation.NonNull;

import java.util.List;

@Component
public class BatchStatusInitializer implements ApplicationListener<ApplicationReadyEvent> {
    
    @Autowired
    private CommentBatchMapper batchMapper;

    @Autowired
    private BatchStatsManager batchStatsManager;
    
    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent event) {
        // 查找所有未完成的批次
        List<CommentBatch> unfinishedBatches = batchMapper.selectList(
            new QueryWrapper<CommentBatch>()
                .in("status", 
                    BatchStatus.PENDING.name(),
                    BatchStatus.PROCESSING.name(),
                    BatchStatus.PARTIALLY_COMPLETED.name())
        );
        
        // 为每个未完成的批次同步状态
        for (CommentBatch batch : unfinishedBatches) {
            batchStatsManager.syncBatchStatus(batch.getBatchNo());
        }
    }
}